package cn.da.shuai.cool.common.web.config;

import cn.da.shuai.cool.common.web.interceptor.PageInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private PageInterceptor pageInterceptor;

    @Resource
    private LocaleChangeInterceptor localeChangeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pageInterceptor);
        registry.addInterceptor(localeChangeInterceptor);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 添加CORS映射
        registry.addMapping("/**")
                // 允许所有源
                .allowedOrigins("*")
                // 允许的方法
                .allowedMethods("*")
                // 允许的请求头
//                .allowCredentials(true)
                // 缓存时间
                .maxAge(60 * 60)
                // 允许的头信息
                .allowedHeaders("*");
    }
}
